<template>
  <div class="app-container p-rel">
    <div class="container-title">查询</div>
    <form-edit
      class="p-abs"
      :detailForm="detailForm"
      :isEdit="isEdit"
      @submitForm="submitForm"
      :editVisible.sync="editVisible"
      v-if="editVisible"
    ></form-edit>
    <el-form
      :model="form"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="85px"
    >
      <el-row :gutter="15">
        <el-col :lg="8" :xl="6">
          <el-form-item label="名称" prop="town">
            <el-input
              v-model="form.enterpriseName"
              placeholder="请输入管理相对人名称查询"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xl="6">
          <el-form-item label="镇/街道" prop="type">
            <el-select
              v-model="form.town"
              multiple
              placeholder="请选择镇/街道"
              clearable
              style="width: 100%"
            >
              <el-option
                v-for="dict in dict.type.enterprise_info_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xl="6">
          <el-form-item label="详细地址" prop="town">
            <el-input
              v-model="form.addr"
              placeholder="请输入详细地址"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>

        <el-col :lg="8" :xl="6">
          <el-button type="primary" icon="" size="" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="" size="" @click="resetQuery">重置</el-button>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8 tableBtnListCss">
      <el-col :span="1.5">
        <!--
        <el-button
          type="primary"
          @click="importData"
          v-hasPermi="['enterpriseInfo:info:export']"
          >导入</el-button
        >
        -->
        
        <el-upload
          action="/dev-api/radiation/extend/importData"
           :headers="{'Authorization': 'Bearer ' }"
          :on-change="handleChange"
          :on-success="handleSuccess"
          :before-upload="beforeUpload"
            :show-file-list="false"
          ref="uploadRef"
          accept="xlsx"
        >
          <el-button size="small" type="primary">导入</el-button>
        </el-upload>
      </el-col>
      <el-col :span="1.5">

        <el-button
          type="primary"
          @click="donwModel"
          v-hasPermi="['enterpriseInfo:info:export']"
          >导入模板下载</el-button
        >


      
      </el-col>
      <el-col :span="1.5">
        <el-button
          @click="synchronous"
          type="primary"
          v-hasPermi="['enterpriseInfo:info:export']"
          >同步工商数据</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="infoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="index" width="55" align="center" label="序号" />
      <el-table-column label="名称" align="center" prop="enterpriseName" />
      <el-table-column
        label="医疗机构执业许可证"
        align="center"
        prop="medicalTreatmentLicense"
      />
      <el-table-column
        label="统一社会信用代码"
        align="center"
        prop="creditCode"
      />
      <el-table-column label="所在镇/街道" align="center" prop="town" />
      <el-table-column label="详细地址" align="center" prop="addr" />
      <el-table-column
        label="是否获取放射诊疗许可证"
        align="center"
        prop="radiationLicenseStatus"
      >
        <template #default="scope">
          {{ scope.row.radiationLicenseStatus == 1 ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column label="今年是否校验" align="center" prop="annualStatus">
        <template #default="scope">
          {{ scope.row.annualStatus == 1 ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="goToDetailPageBtn(scope.row, 0)"
            >人员管理</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="goToDetailPageBtn(scope.row, 1)"
            >设备管理</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="goToDetailPageBtn(scope.row, 2)"
            >查看现状评价或定期检测报告</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="goToDetailPageBtn(scope.row, 3)"
            >查看教育培训</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pageNum"
      :limit.sync="pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  updateInfo,
  extendList,
  dataSynchronization,
  importData,
  exportData,
} from "@/api/zhangping/zpEnterpriseInfo";
import { download } from "@/utils/request.js";
import { categoryList } from "@/api/zhangping/category";
import formEdit from "./editFrom.vue";
import { Message } from "element-ui";
import { getToken } from '@/utils/auth'
export default {
  name: "Info",
  dicts: [
    "enterprise_info_type",
    "health_certificate_status",
    "business_file_status",
    "enterprise_category",
  ],
  components: { formEdit },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 一企一档表格数据
      infoList: [
        {
          A: "漳平市西园镇卫生院",
          B: "56156156112114",
          C: "56156156112114",
          D: "西园镇",
          E: "福建省龙岩市漳平市西园乡西中路3号",
          F: "是",
          G: "是",
        },
        {
          A: "漳平优抚医院",
          B: "56156156112114",
          C: "56156156112114",
          D: "箐城街道",
          E: "福建省龙岩市漳平市和富路52号",
          F: "是",
          G: "是",
        },
        {
          A: "漳平市中医院",
          B: "56156156112114",
          C: "56156156112114",
          D: "箐城街道",
          E: "福建省龙岩市漳平市",
          F: "否",
          G: "否",
        },
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      //页码
      pageNum: 1,
      pageSize: 10,
      // 表单参数
      form: {
        enterpriseName: "",
        town: "",
        addr: "",
      },
      // 表单校验
      rules: {},
      detailForm: {},
      isEdit: false,
      editVisible: false,
      categoryList: [],
      fileList: [],
      token:''
    };
  },
  created() {
    this.token = getToken()
    this.loading = false;
    this.getList();
    // this.getCategoryList()
  },
  methods: {
    // 文件状态改变时的钩子
    handleChange(file, fileList) {
      console.log('当前文件:', file);
      console.log('所有文件:', fileList);
    },
    
    // 上传成功时的回调
    handleSuccess(response, file, fileList) {
      console.log('上传成功:', response);
      this.$message.success('上传成功');
    },
    
    // 上传前的钩子，可以用于校验和预处理
    beforeUpload(file) {
     return true
    },
    goToDetailPageBtn(row, type) {
      const typeMap = {
        0: "personDetail",
        1: "equipmentDetail",
        2: "reportDetail",
        3: "trainDetail",
      };
      const obj = {
        path: `/radiationHygiene/test/${typeMap[type]}`,
        query: {
          id: row.creditCode,
        },
      };
      this.$router.push(obj);
    },
    /** 查询一企一档列表 */
    async getList() {
      this.loading = true;
      // listInfo(this.queryParams).then(response => {
      //   this.infoList = response.rows
      //   this.total = response.total;
      //   this.loading = false;
      // });
      let res = await extendList({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        ...this.form,
      });
      if (res.code == 200) {
        this.infoList = res.rows;
        this.total = res.total;
        this.loading = false;
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        enterpriseName: "",
        town: "",
        addr: "",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.detailForm = {};
      this.isEdit = false;
      this.editVisible = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      // this.reset();
      const id = row.id || this.ids;
      getInfo(id).then((response) => {
        this.detailForm = response.data;
        if (type == 1) {
          this.isEdit = true;
        } else {
          // 查看
          this.isEdit = false;
        }
        this.editVisible = true;
      });
    },
    /** 提交按钮 */
    submitForm(form, id) {
      if (id != null) {
        updateInfo(form).then((response) => {
          this.$modal.msgSuccess("修改成功");
          this.editVisible = false;
          this.getList();
        });
      } else {
        addInfo(form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.editVisible = false;
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除一企一档编号为"' + ids + '"的数据项？')
        .then(function () {
          return delInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导入按钮操作 */
    handleExport() {
      // alert("待做")
    },
    getCategoryList() {
      categoryList().then((response) => {
        this.categoryList = response.data;
      });
    },
    // 导入
    async importData() {},
    //下载导入模板
    async donwModel() {
      let res = await download("/radiation/extend/export", {}, "模板.xlsx", {});
      console.log(res);
    },
    // 同步工商数据
    async synchronous() {
      let res = await dataSynchronization();
      if (res.code == 200) {
        Message.success("同步成功");
      } else {
        Message.error(res.msg);
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.app-container {
  .tableBtnListCss {
    margin-top: 10px;
    padding-top: 10px;
  }
}
</style>
